/**
 * Project Name:mauto-web
 * File Name:AreaService.java
 * Package Name:com.inftech.mauto.sys.service
 * Date:2016年10月7日下午9:14:13
 * Copyright (c) 2016, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.core.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.inftech.mauto.common.base.BusinessException;
import com.inftech.mauto.common.constant.BusinessConstant;
import com.inftech.mauto.common.constant.HttpStatusCodeEnum;
import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.core.dao.ICommentDao;
import com.inftech.mauto.core.dao.IEnquiryDao;
import com.inftech.mauto.core.dao.IOrderDao;
import com.inftech.mauto.core.entity.Comment;
import com.inftech.mauto.core.entity.Enquiry;
import com.inftech.mauto.core.entity.Order;
import com.inftech.mauto.sys.dao.ICustomerServiceDao;
import com.mingsoft.base.biz.impl.BaseBizImpl;
import com.mingsoft.base.dao.IBaseDao;
import com.mingsoft.weixin.entity.WeixinPeopleEntity;

/**
 * 
 * ClassName: CommentService <br/>
 * Function: 评价service. <br/>
 * date: 2016年11月6日 下午10:24:00 <br/>
 *
 * @author puyf
 * @version
 * @since JDK 1.7
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service("commentService")
public class CommentService extends BaseBizImpl {

	/**
	 * 评价持久化层注入
	 */
	@Autowired
	private ICommentDao commentDao;

	/**
	 * 咨询持久化层注入
	 */
	@Autowired
	private IEnquiryDao enquiryDao;

	/**
	 * 客服持久化层注入
	 */
	@Autowired
	private ICustomerServiceDao customerServiceDao;

	/**
	 * 订单持久化层注入
	 */
	@Autowired
	private IOrderDao orderDao;

	/**
	 * 订单业务层注入
	 */
	@Autowired
	private OrderService orderService;

	@Override
	protected IBaseDao getDao() {
		return commentDao;
	}

	/**
	 * 
	 * saveComment:新增评价. <br/>
	 *
	 * @author puyf
	 * @param comment
	 * @param people
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public void saveComment(Comment comment, WeixinPeopleEntity people) throws Exception {
		int peopleId = people.getPeopleId();
		String openId = people.getWeixinPeopleOpenId();
		Date now = CommonUtils.getNowD();
		Map<String, Object> whereMap = new HashMap<String, Object>();
		whereMap.put("id", comment.getObjectId());
		whereMap.put("del_flag", 0);
		// 更新相关业务表
		if (comment.getCommentType().equals(BusinessConstant.COMMENT_TYPE_BP)) {
			// 钣喷业务
			// 通过对象id获取对象
			Order orderDB = orderDao.getEntity(whereMap);
			// 数据操作权限判断
			if (!people.getPeopleRole().equals(BusinessConstant.PEOPLE_ROLE_C)
					|| !orderDB.getPeopleId().equals(peopleId)) {
				LOG.error("评价订单失败，用户没有权限");
				throw new BusinessException(HttpStatusCodeEnum.STATUS_CODE_403);
			}
			// 状态判断(待评价)
			String osStr = OrderService.getOsStr(orderDB);
			if (!osStr.equals(BusinessConstant.OSSTR_DPJ)) {
				LOG.error("评价订单失败，订单状态不正确");
				throw new BusinessException(HttpStatusCodeEnum.STATUS_CODE_941);
			}
			// 更新订单
			Order order = new Order();
			order.setOrderCode(orderDB.getOrderCode());
			order.setOrderStatus(BusinessConstant.OS_YWC);
			order.setOrderCommentStatus(BusinessConstant.OCS_YPJ);
			order.setOrderCommentTime(now);
			order.setUpdateDate(now);
			order.setUpdateBy(people.getPeopleId());
			orderDao.updateEntity(order);
			// 插入操作记录表
			orderService.saveOrderAction(BusinessConstant.ORDER_ACTION_COMMENT, peopleId, now,
					Integer.parseInt(orderDB.getId()), BusinessConstant.OS_YWC, orderDB.getOrderCheckStatus(),
					orderDB.getOrderConfirmStatus(), orderDB.getOrderPayStatus(), orderDB.getOrderSignStatus(),
					orderDB.getOrderFollowStatus(), BusinessConstant.OCS_YPJ);
		} else if (comment.getCommentType().equals(BusinessConstant.COMMENT_TYPE_ZX)) {
			// 车大夫咨询业务
			// 通过对象id获取对象
			Enquiry enquiry = enquiryDao.getEntity(whereMap);

			// 数据操作权限判断
			if (!enquiry.getEnquiryOpenId().equals(openId)) {
				LOG.error("在线咨询评价失败，用户没有权限");
				throw new BusinessException(HttpStatusCodeEnum.STATUS_CODE_403);
			}
			// 数据状态判断
			if (enquiry.getEnquiryCommentStatus().equals(BusinessConstant.COMMENT_STATUS_ON)) {
				LOG.error("在线咨询评价失败，不能重复评价");
				throw new BusinessException(HttpStatusCodeEnum.STATUS_CODE_921);
			}
			
			// 更新咨询表
			enquiry = new Enquiry();
			enquiry.setId(String.valueOf(comment.getObjectId()));
			enquiry.setEnquiryCommentTime(now);
			enquiry.setEnquiryCommentStatus(BusinessConstant.COMMENT_STATUS_ON);
			enquiry.setUpdateBy(peopleId);
			enquiry.setUpdateDate(now);
			enquiryDao.updateEntity(enquiry);

			// 更新客服表评分
			/*String csAccount = enquiry.getEnquiryKfAccount();
			whereMap.clear();
			whereMap.put("csAccount", csAccount);
			whereMap.put("updateBy", peopleId);
			whereMap.put("updateDate", now);
			whereMap.put("commentType", BusinessConstant.COMMENT_TYPE_ZX);
			customerServiceDao.updateCustomerServiceScore(whereMap);*/
		}
		// 插入评价信息表
		comment.setPeopleId(peopleId);
		comment.setCreateBy(peopleId);
		comment.setCreateDate(now);
		comment.setUpdateDate(now);
		comment.setCommentTime(now);
		comment.setDelFlag(0);
		commentDao.saveEntity(comment);
	}

	/**
	 * 
	 * queryCommentList:查询评价列表. <br/>
	 *
	 * @author puyf
	 * @param people
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public List<Map<String, Object>> queryCommentListFront(WeixinPeopleEntity people) throws Exception {
		// 返回列表
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		int peopleId = people.getPeopleId();
		Byte peopleRole = people.getPeopleRole();
		if (peopleRole.equals(BusinessConstant.PEOPLE_ROLE_C)) {
			// 车主角色
			// 查询我的评价列表
			Map<String, Object> whereMap = new HashMap<String, Object>();
			whereMap.put("peopleId", peopleId);
			List<Comment> commentList = commentDao.queryCommentList(whereMap);
			if (commentList != null && commentList.size() != 0) {
				for (Comment comment : commentList) {
					Map<String, Object> commentM = new HashMap<String, Object>();
					// 评价对象
					commentM.put("commentObject", comment.getCommentObject());
					// 评价类型
					commentM.put("commentType", comment.getCommentType());
					// 评价内容
					commentM.put("commentContent", comment.getCommentContent());
					// 评分
					commentM.put("commentScore", comment.getCommentScore());
					// 评价时间
					commentM.put("CommentTime", CommonUtils.formatDate1(comment.getCommentTime()));
					resultList.add(commentM);
				}
			}
		} else if (peopleRole.equals(BusinessConstant.PEOPLE_ROLE_S)) {
			// 服务单位角色
			// 查询我收到的评价列表
			Map<String, Object> whereMap = new HashMap<String, Object>();
			whereMap.put("commentType", BusinessConstant.COMMENT_TYPE_BP);
			whereMap.put("enterpriseId", people.getPeopleEnterpriseId());
			resultList = commentDao.queryCommentListEnterprise(whereMap);
			if (resultList != null && resultList.size() != 0) {
				for (Map<String, Object> resultMap : resultList) {
					resultMap.put("commentTime", CommonUtils.formatDate1((Date) resultMap.get("commentTime")));
				}
			}
		}
		return resultList;
	}
	
	/**
	 * 
	 * queryCommentList:后台查询评价列表. <br/>
	 *
	 * @author puyf
	 * @param paramMap
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public List<Comment> queryCommentList(Map<String, Object> paramMap) throws Exception {
		// 获取评价列表
		List<Comment> commentList = commentDao.queryCommentList(paramMap);
		return commentList;
	}

	/**
	 * 
	 * queryComment:查询评价. <br/>
	 *
	 * @author puyf
	 * @param objectId
	 * @param commentType
	 * @return
	 * @throws Exception
	 * @since JDK 1.7
	 */
	public Comment queryComment(String objectId, Byte commentType) throws Exception {
		// 查询我的评价列表
		Map<String, Object> whereMap = new HashMap<String, Object>();
		whereMap.put("objectId", objectId);
		whereMap.put("commentType", commentType);
		List<Comment> commentList = commentDao.queryCommentList(whereMap);
		if (commentList != null && commentList.size() == 1) {
			return commentList.get(0);
		} else {
			return null;
		}
	}
}
